package net.mihome.app.adapter;

import android.R.integer;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.BitmapFactory;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import net.mihome.app.AppContext;
import net.mihome.app.bean.ChatMsgEntity;
import net.mihome.app.bean.URLs;
import net.mihome.app.common.BitmapManager;
import net.mihome.app.R;

public class ChatMsgViewAdapter extends BaseAdapter {

	private BitmapManager bmpManager;
	private AppContext appContext;
	//ListView视图的内容由IMsgViewType决定
	public static interface IMsgViewType
	{
		//对方发来的信息
		int IMVT_COM_MSG = 0;
		//自己发出的信息
		int IMVT_TO_MSG = 1;
	}
	
    private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();
    private List<ChatMsgEntity> data;
    private Context context;  
    private LayoutInflater mInflater;

    public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> data) {
        this.context = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
        this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(
				context.getResources(), R.drawable.widget_dface_loading));
        appContext=(AppContext) context.getApplicationContext();
    }

    //获取ListView的项个数
    public int getCount() {
        return data.size();
    }

    //获取项
    public Object getItem(int position) {
        return data.get(position);
    }

    //获取项的ID
    public long getItemId(int position) {
        return position;
    }

    //获取项的类型
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
	 	ChatMsgEntity entity = data.get(position);
	 	
	 	if (entity.getMsgType())
	 	{
	 		return IMsgViewType.IMVT_COM_MSG;
	 	}else{
	 		return IMsgViewType.IMVT_TO_MSG;
	 	}
	 	
	}

	//获取项的类型数
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}
	
	//获取View
    public View getView(int position, View convertView, ViewGroup parent) {
    	
    	ChatMsgEntity entity = data.get(position);
    	String userName=appContext.getLoginInfo().getName();
    	boolean isComMsg = entity.getMsgType();
    	ViewHolder viewHolder = null;	
//	    if (convertView == null)
//	    {
	    	  if (!userName.equals(entity.getName()))
			  {
	    		  //如果是对方发来的消息，则显示的是左气泡
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);
			  }else{
				  //如果是自己发出的消息，则显示的是右气泡
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
			  }
	    	  if(!entity.getMsgType()){
	    		//如果是自己发出的消息，则显示的是右气泡
				  convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);
	    	  }
	    	  viewHolder = new ViewHolder();
			  viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
			  viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
			  viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
			  viewHolder.avatarImageView=(ImageView) convertView.findViewById(R.id.iv_userhead);
			  viewHolder.isComMsg = isComMsg;

			  convertView.setTag(viewHolder);
//	    }else{
//	        viewHolder = (ViewHolder) convertView.getTag();
//	    }
	    viewHolder.tvSendTime.setText(entity.getDate());
	    viewHolder.tvUserName.setText(entity.getName());
	    viewHolder.tvContent.setText(entity.getText());
	    if(entity.getAvatar()!=null){
	    	bmpManager.loadBitmap(entity.getAvatar(), viewHolder.avatarImageView);
	    }
	    return convertView;
    }
    
    //通过ViewHolder显示项的内容
    static class ViewHolder { 
    	public ImageView avatarImageView;
        public TextView tvSendTime;
        public TextView tvUserName;
        public TextView tvContent;
        public boolean isComMsg = true;
    }
    
}
